Ccay.Module.define("ccay.security.role.roleOperationView",function(page,$S){
    var treeBeforeCheck=true;
    var treeObject;
    //直接页面URL访问
    page.ready = function(){
		var request=Ccay.Page.getRequest();
		var roleId = request["roleId"];
		if(roleId){
			page.initById(roleId);	
		};
	}
	//通过ID初始化
	page.initById = function(roleId){
		Ccay.doGet("services/manage/security/roleManage/findRoleById/"+roleId,null,function(role){
				page.init(role);
		});
	}
	//通过对象初始化
	page.init=function(role){
		page.initRole(role);
		page.findResourceList(role.id);
		
	};
    //zTree setting
    var setting = {
				check : {
					enable : true,
					beforeCheck : false,
					chkboxType : {
						"Y" : "ps",
						"N" : "ps"
					}
				},
				data : {
					key : {
						name : "name",
						children : "children",
						title : "title",
						checked : "checked"
					}
				},
				callback : {
					beforeCheck : function() {
						return treeBeforeCheck;
					}
				}
			};

	
	/**
	 * @description 初始化父元素
	 */
	page.initRole = function(role){
		$S("#roleId").val(role.id);
		$S("#roleName").text(role.name);
		if(role.description){
			$S("#roleDesc").text(role.description);
		}
	};
	
	/** 
	 * 获取权限列表 绘制树
	 */
	page.findResourceList = function(roleId){
		Ccay.doGet("services/manage/security/permissionManage/findPermissionTree",{roleId:roleId},function(resources){
			//左侧树初始化
			treeObject = Ccay.tree.init($("#permissionSelectTree"), setting, resources,false,null);
			treeObject.expandAll(true);
			var nodes = treeObject.getNodes();
			for (var i=0, l=nodes.length; i < l; i++) {
				treeObject.setChkDisabled(nodes[i], true,true,true);
			}
			//右侧列表初始化
			Role.initHasPermissionList(resources,"#hasPermissionsList");
		});
	};

   	/**
	 * 递归获取条目 
	 */
   page.permissionList = function(children,parentEntry){
   		var result = [];
   		var opt = [];
		for(var i=0; i<children.length; i++){
			if(children[i].permissionString && children[i].checked){
				opt.push(children[i].name);
			}else{
				var path = [];
				for(var k=0;k<parentEntry.path.length;k++){
					path.push(parentEntry.path[k])
				}
				path.push(children[i].name);
				var entry = {
					path : path,
					opt : parentEntry.opt
				};
				var childrenEntries = page.permissionList(children[i].children,entry)
				for(var j=0;j<childrenEntries.length;j++){
					result.push(childrenEntries[j]);
				}
			}
		}
		if(opt.length != 0){
			parentEntry.opt = opt;
			result.push(parentEntry);
		}
		return result;
   }
   
});
